package cn.midas.org.service.impl;

import cn.midas.basic.service.impl.BaseServiceImpl;
import cn.midas.basic.utils.MD5Utils;
import cn.midas.basic.utils.StrUtils;
import cn.midas.org.domain.Employee;
import cn.midas.org.mapper.EmployeeMapper;
import cn.midas.org.service.IEmployeeService;
import cn.midas.user.domain.Logininfo;
import cn.midas.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* @Description: 部门业务层实现类
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/21 16:42
*/
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void save(Employee employee) {
        // 初始化employee
        initEmployee(employee);
        // 使用工具类将employee数据拷贝到logininfo中
       Logininfo logininfo = employee2Logininfo(employee);
       //添加logininfo
       logininfoMapper.save(logininfo);
       // 添加employee
       employeeMapper.save(employee);
       //将logininfo主键id给logininfo的setLogininfoId
       employee.setLogininfoId(logininfo.getId());
       // 重新添加employee
       employeeMapper.save(employee);
       // 初始化关联表数据
        initRelationTable(employee);
    }

    private void initRelationTable(Employee employee) {
        // 1.根据用户Id,角色删除用户角色关联表的所有数据
        employeeMapper.removeEmployeeRole(employee.getId());
        // 根据用户Id,角色Id集合新增关联表数据
        employeeMapper.saveEmployeeRole(employee.getId(), employee.getRoles());
    }

    @Override
    public void remove(Long id) {
        employeeMapper.remove(id);
        Employee employee = employeeMapper.loadById(id);
        Long logininfoId = employee.getLogininfoId();
        logininfoMapper.remove(logininfoId);

    }

    @Override
    public void update(Employee employee) {
        initEmployee(employee);
        employeeMapper.update(employee);
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.update(logininfo);
    }

    @Override
    public void batchDel(Long[] ids) {
        employeeMapper.batchDel(ids);
        for (Long id : ids) {
            Employee employee = employeeMapper.loadById(id);
            Long logininfoId = employee.getLogininfoId();
            logininfoMapper.remove(logininfoId);
        }
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5( employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }
}
